﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Financial;
using LuxDiner.Logic.Financial;
namespace LuxDiner.Web.Management.Areas.Financial.Controllers
{
    [RouteArea("Financial")]
    [RoutePrefix("Payment")]
    public class PaymentApiController : ApiController
    {
        [Route("GetTops")]
        [HttpPost]
        public JsonEntitySet<PaymentModel> GetTops(JObject request)
        {
            string errinfo = "no data";
            try
            {
                PaymentQuery sea = new PaymentQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.CustomerID = request.Value<int>("customerID");
                sea.GetCustomer = request.Value<bool>("getCustomer");
                sea.HideZero = request.Value<bool>("hideZero");
                sea.GetTotalBalance = request.Value<bool>("getTotalBalance");
                sea.CustomerType = request.Value<int>("customerType");
                var result = PaymentLogic.GetTopPayments(sea);
                return new JsonEntitySet<PaymentModel>(result);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<PaymentModel>(null, errinfo, false);
        }
        [Route("GetPayments")]
        [HttpPost]
        public JsonEntitySet<PaymentModel> GetPayments(JObject request)
        {
            string errinfo = "no data";
            try
            {
                PaymentQuery sea = new PaymentQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.CustomerID = request.Value<int>("customerID");
                sea.OrderNo = request.Value<string>("orderNo");
                var ndb = request.Value<string>("noteDateBefore");
                if (!string.IsNullOrEmpty(ndb))
                    sea.NoteDateBefore = DateTime.Parse(ndb);
                var nda = request.Value<string>("noteDateAfter");
                if (!string.IsNullOrEmpty(nda))
                    sea.NoteDateAfter = DateTime.Parse(nda);
                sea.GetCustomer = request.Value<bool>("getCustomer");

                var result = PaymentLogic.GetPayments(sea);
                return new JsonEntitySet<PaymentModel>(result);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<PaymentModel>(null, errinfo, false);
        }
        [Route("GetTotalBalance")]
        [HttpPost]
        public JsonResult<decimal[]> GetTotalBalance(JObject request)
        {
            string errinfo = "no data";
            try
            {
                var result = PaymentLogic.GetTotalBalance();
                return new JsonResult<decimal[]>(result);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<decimal[]>(null, errinfo, false);
        }
        [Route("GetBankWater")]
        [HttpPost]
        public JsonEntitySet<VBankWaterModel> GetBankWater(JObject request)
        {
            string errinfo = "no data";
            try
            {
                VBankWaterQuery sea = new VBankWaterQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.BankID = request.Value<int>("bankID");
                sea.GetCustomer = request.Value<bool>("getCustomer");

                var result = VBankWaterLogic.GetList(sea);
                return new JsonEntitySet<VBankWaterModel>(result);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<VBankWaterModel>(null, errinfo, false);
        }
    }
}
